// pages/exchange/exchange.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initialplace: "",//可兑换钻石
    toinfo:"",//兑换人信息
    toinfoShow:false,//兑换人信息是否显示
    modifyShow:false,//修改兑换人是否显示
    exchangeNum:"",//兑换金额
    toinfochange:"",//修改兑换人信息
    toinfochange1:"",//修改兑换人信息1
    touserPerfect:""//修改兑换人id
  },
  /**
   * 显示修改兑换人
   */
  modify(){
    this.setData({
      modifyShow:true
    })
  },
   /**
   * 关闭修改兑换人
   */
  modifyClose(){
    this.setData({
      modifyShow:false,
      touserPerfect:"",
    })
  },
   /**
   * 防止事件冒泡
   */
  modifyView(){},
   /**
   * 更改兑换金额
   */
  exchangeInput(data){
    this.setData({
      exchangeNum:data.detail.value
    })
  },
   /**
   * 搜索兑换人
   */
  modifyInfoIpt(data){
    this.setData({
      touserPerfect:data.detail.value
    })
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&perfectNumber=" +
      this.data.touserPerfect +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      perfectNumber:this.data.touserPerfect,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/getUserbyNumber",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          toinfochange1: res.data.result
        })
    })
  },
  /**
   * 修改兑换人
   */
  changemodify(){
    this.setData({
      toinfochange:this.data.toinfochange1,
      toinfoShow:true,
      modifyShow:false,
      touserPerfect:"",
      toinfochange1:""
    })
  },
 /**
   * 兑换
   */
  exchangeCharm(){
    let userid
    if(this.data.toinfochange.user_id){
      userid=this.data.toinfochange.user_id
    }else{
      userid=this.data.toinfo.userId
    }
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&num=" +
      this.data.exchangeNum +
      "&memberId=" +
      userid +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      num:this.data.exchangeNum,
      memberId:userid,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/balanceDiamond",
      data: params
    }).then((res) => {
      console.log(res.data.result),
      wx.showToast({
        title: res.data.text,
        icon:"none"
      })
        this.setData({
          exchangeNum:""
        })
        setTimeout(()=>{
          this.getcharm()
        },1500)
    })
  },
 /**
   * 获取可兑换魅力值
   */
  getcharm(){
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/userInfo",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          initialplace: "可兑换" + res.data.result.user.wagesMoney + "积分",
        })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/userInfo",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          initialplace: "可兑换" + res.data.result.user.wagesMoney + "元",
          toinfo:res.data.result.user
        })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})